import { AddDate, ArticleDate } from '@/api/Ljq/index';
import { message } from 'antd';

export default {
  namespace: 'AllDate',
  state: {
    //文章分类
    list: [],
    //文章所有内容】
    ArticleList: [],
    //搜索
    Searchlist:[]
  },

  //同步方法
  reducers: {
    //获取文章分类
    setKnowledge(state: any, { payload }: any) {
      state.list = payload;
    },
    //获取所有文章
    setArticle(state: any, { payload }: any) {
      state.ArticleList = payload;
      state.Searchlist = payload;
    },
    //所有文章页搜索
    setSearch(state: any, { payload }: any) {
      state.ArticleList = state.ArticleList.filter((item:any, index:number) => {
        return item.title.includes(payload.title) && item.category.label.includes(payload.classify)
      })
    },

    //所有文章页删除
    setDel(state: any, { payload }: any) {
      console.log(payload.id)
      state.ArticleList.forEach((item:any, index:number) => {
        console.log(item.id)
        if(item.id===payload.id){
          state.ArticleList.splice(index, 1)
        }
      })
    },
  },
  //异步方法
  effects: {
    //获取文章分类数据
    *AddDate({ payload }: any, { call, put }: any): any {
      //获取接口数据
      let result: any = yield call(AddDate, payload);
      yield put({
        type: 'setKnowledge',
        payload: result.data.data,
      });
    },
    //获取所有文章
    *getArticle({ payload }: any, { call, put }: any): any {
        //获取接口数据
        let result: any = yield call(ArticleDate, payload);
        yield put({
          type: 'setArticle',
          payload: result.data.data[0],
        });
      },

      //接收传递过来的搜索
    *SEARCH({ payload }: any, { call, put }: any): any {
      const {title,state,classify}=payload
      let result: any = yield call(ArticleDate, title,state,classify);
      yield put({
        type: 'setSearch',
        payload: {title,state,classify},
      });
    },
     //所有文章页删除
     *DEL({ payload }: any, { call, put }: any): any {
      const {id}=payload
      let result: any = yield call(ArticleDate, id);
      yield put({
        type: 'setDel',
        payload: {id},
      });
    },
  },
};
